<?php

namespace app\common\model\kzvote;

use addons\kzvote\library\Subscription;
use addons\kzvote\library\WechatSubscription;
use think\Model;


class Person extends Model
{

    

    

    // 表名
    protected $name = 'kzvote_person';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'createtime_text',
    ];

    protected static function init()
    {
        $platform = request()->param('platform');
        self::afterInsert(function ($row) {
            self::updateVote($row['vote_id']);
        });
        self::afterUpdate(function ($row) use ($platform){
            self::updateVote($row['vote_id']);

            if ($row->origin && $row->origin['status'] != $row->data['status'] && $row['user_id'] > 0){
                //审核结果有变动则进入到这里，发送订阅消息通知
                $vote = Vote::get($row['vote_id']);
                if ($row->data['status'] == '10'){
                    $status = '请耐心等待审核';
                }else if ($row->data['status'] == '20'){
                    $status = '审核通过';
                }else{
                    $status = '审核驳回';
                }
                if ($platform == 'wxapp'){
                    Subscription::apply_check(kzvote_getOpenid($row['user_id'],$platform),'pages/vote/person?person_id='.$row['id'],$vote['name'],
                        $row['name'],$status);
                }elseif ($platform == 'wechat'){
                    WechatSubscription::apply_check(kzvote_getOpenid($row['user_id'],$platform),'pages/vote/person?person_id='.$row['id'],$vote['name'],
                        $row['name'],$status);
                }
            }

        });
        self::afterDelete(function ($row) {
            self::updateVote($row['vote_id']);
        });
    }

    private static function updateVote($vote_id){
        //修正投票数据的人数
        $result             = self::where(['vote_id' => $vote_id])->where('status', '20')
            ->field('count(*) as person_count,sum(count) as count')->find();
        $vote               = \app\common\model\kzvote\Vote::get($vote_id);
        $vote->person_count = $result['person_count'];
        $vote->count        = $result['count'];
        $vote->save();
    }
    
    public function getStatusList()
    {
        return ['10' => __('Status 10'), '20' => __('Status 20'), '30' => __('Status 30')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function setOptionAttr($value){
        if ($value){
            return json_encode($value);
        }
        return null;
    }

    public function getOptionAttr($value)
    {
        if ($value){
            return json_decode($value,true);
        }
        return null;
    }

    public function getErCodeAttr($value)
    {
        if ($value){
            return request()->domain() . $value;
        }
        return null;
    }
    public function getImageAttr($value,$data){
        if (!$value){
            //获取项目域名
            $domain = request()->domain();
            return $domain.'/assets/addons/kzvote/img/pk.png';
        }else{
            return $value;
        }
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function vote()
    {
        return $this->belongsTo('Vote', 'vote_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function music(){
        return $this->belongsTo('app\common\model\kzvote\music\Music', 'music_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
